2024 Method
Changes 0
M

Document.Delete

Description:
Deletes a set of elements from the document.
Remarks:
This method will delete the elements and any elements that are totally dependent upon that element. Any references to the deleted elements will become invalid and hence cause an exception to be thrown if they are accessed. The elements will be deleted with no prompts for user confirmation. Pinned elements will be deleted with no warnings.

Note: in a family document, the predefined elements (those elements inherited from its family template file) can't be deleted by this method.

Overloads (2):
Delete(ICollection[ElementId])
public ICollection<ElementId> Delete(
	ICollection<ElementId> elementIds
)
  • ICollection<ElementId>
    elementIds
    The ids of the elements to delete.
Return Value ICollection<ElementId> The deleted element id set.
// Delete all the selected elements via the set of element ids.
ICollection<Autodesk.Revit.DB.ElementId> idSelection = null ;
 UIDocument uidoc = new UIDocument(document);
 ICollection<ElementId> selectedIds = uidoc.Selection.GetElementIds();
 foreach (ElementId id in selectedIds)
 {
     idSelection.Add(id);
 }
 ICollection<Autodesk.Revit.DB.ElementId> deletedIdSet = document.Delete(idSelection);

 if (0 == deletedIdSet.Count)
 {
     throw new Exception("Deleting the selected elements in Revit failed.");
 }

 TaskDialog.Show("Revit","The selected element has been removed.");